/*!
 * \file test_http.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */

#include <stdio.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include <pthread.h>

#include "t_frcomm/t_frcomm.h"
#include "t_frxml_parse/t_frxml_parse.h"
#include "t_frhex_dump/t_frhex_dump.h"
#include "t_frlog/t_frlog.h"
#include "t_frconfig/t_frconfig.h"
#include "t_frhash2/t_frhash2.h"
#include "t_frhashmap_varfield/t_frhashmap_varfield.h"
#include "t_frlist/t_frlist.h"
#include "t_frstr/t_frstr.h"
#include "t_frsutl/t_frsutl.h"
#include "t_iconv2utf8/t_iconv2utf8.h"
//#include "t_frmem_stat/t_frmem_stat.h"
#include "t_frbyte_buffer/t_frbyte_buffer.h"
#include "t_frjson_checker/t_frjson_checker.h"


//todo frmem_stat UT

static Frmemstat_mng g_st_memstat_mng={0,FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t  g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

void add_suites(FunType *p_func, int i_suites_len, FunType fp)
{
    int i_loop = 0;
    for (;i_loop < i_suites_len; i_loop++)
    {
        if (p_func[i_loop] == NULL)
        {
            p_func[i_loop] = fp;
            break;
        }
    }
}

int main(int argc, char **argv)
{
    if (fr_slab_init(&g_st_mem_env, "apmcomm") == NULL)
        return FR_FAIL;

    apmtest_cunit_registry();

    FunType fun_suites[1024];
    memset(fun_suites, 0x00, sizeof(fun_suites));
    int i_suites_len = sizeof(fun_suites) / sizeof(FunType);
    int i_loop = 0;

    // 加载各个测试组件
    // t_frcomm/t_frcomm.h
    add_suites(fun_suites, i_suites_len, add_suite___frtime2str);
    add_suites(fun_suites, i_suites_len, add_suite_frdbg_print);

    //t_frxml_parse/t_frxml_parse
    add_suites(fun_suites, i_suites_len, add_suite_frxml_del2doc_one_cont);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_del2doc_all_cont);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_detect2doc_string);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get2ptr_string);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get2doc_prop_string);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get2doc_int);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get_int_list);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get2doc_string);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_get_string_list);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_insert2doc_content);
    add_suites(fun_suites, i_suites_len, add_suite_frxml_update2doc_content);

    // t_frhex_dump/t_frhex_dump
    add_suites(fun_suites, i_suites_len, add_suite_frhex_dump_to_buffer);

    //t_frlog/t_frlog
    add_suites(fun_suites, i_suites_len, add_suite_frlog_debug1);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_debug2);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_error1);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_error2);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_fatal1);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_fatal2);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_info1);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_info2);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_warn1);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_warn2);
    add_suites(fun_suites, i_suites_len, add_suite_frlog_open);

    //t_frconfig
    add_suites(fun_suites, i_suites_len, add_suite___frtrim);
    add_suites(fun_suites, i_suites_len, add_suite_t_frconfig_del_string);
    add_suites(fun_suites, i_suites_len, add_suite_frconfig_int);
    add_suites(fun_suites, i_suites_len, add_suite_frconfig_string);
    add_suites(fun_suites, i_suites_len, add_suite_frconfig_set_int);
    add_suites(fun_suites, i_suites_len, add_suite_frconfig_set_string);
    add_suites(fun_suites, i_suites_len, add_suite___get_arr_line);
    add_suites(fun_suites, i_suites_len, add_suite___split_eq_int);
    add_suites(fun_suites, i_suites_len, add_suite___split_eq_string);

    //t_frhash2/t_frhash2.h
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_addLong);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_addStr);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_create);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_create2);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_del);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_delLong);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_delStr);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_find);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_find_strKey);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_free);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_free2);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_getLong);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_getStr);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_run);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_show);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_show_all);
    add_suites(fun_suites, i_suites_len, add_suite_frhash2_show_all2);
    add_suites(fun_suites, i_suites_len, add_suite_frhash_key);

    //t_frhashmap_varfield/t_frhashmap_varfield.h
    add_suites(fun_suites, i_suites_len, add_suite___frhashmap_varfield_free);
    add_suites(fun_suites, i_suites_len, add_suite___frhashmap_varfield_show);
    add_suites(fun_suites, i_suites_len, add_suite_frhashmap_varfield_add);

    //t_frlist
    add_suites(fun_suites, i_suites_len, add_suite_frlist_add_header);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_add_sort);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_add_tail);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_create);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_destroy);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_empty);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_find);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_header_init);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_merge_header_top);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_node_apart);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_node_create);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_node_data_free);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_node_free);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_node_idx);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_remove_all);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_remove_from_idx);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_remove_head);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_run);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_shallow_copy);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_show);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_show_all);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_show_all2);
    add_suites(fun_suites, i_suites_len, add_suite_frlist_size);

    //t_frstr
    add_suites(fun_suites, i_suites_len, add_suite_frstr_append);
    add_suites(fun_suites, i_suites_len, add_suite_frstr_free);
    add_suites(fun_suites, i_suites_len, add_suite_frstr_free2);
    add_suites(fun_suites, i_suites_len, add_suite_frstr_new);
    add_suites(fun_suites, i_suites_len, add_suite_frstr_trunc);

    //t_frsutl
    add_suites(fun_suites, i_suites_len, add_suite_fris_print_ascii);
    add_suites(fun_suites, i_suites_len, add_suite_frmemstr);
    add_suites(fun_suites, i_suites_len, add_suite_frrtrimLF);
    add_suites(fun_suites, i_suites_len, add_suite_frstrlower);
    add_suites(fun_suites, i_suites_len, add_suite_frstrupper);
    add_suites(fun_suites, i_suites_len, add_suite_frget_absdir);
    add_suites(fun_suites, i_suites_len, add_suite_frltrim);
    add_suites(fun_suites, i_suites_len, add_suite_frprint_bin);
    add_suites(fun_suites, i_suites_len, add_suite_frrtrim);
    add_suites(fun_suites, i_suites_len, add_suite_frtime2str);
    add_suites(fun_suites, i_suites_len, add_suite_frtime_diff);
    add_suites(fun_suites, i_suites_len, add_suite_frtrim);
    add_suites(fun_suites, i_suites_len, add_suite_iptools_parse_ipaddr);
    add_suites(fun_suites, i_suites_len, add_suite_iptools_parse_iprange);
    add_suites(fun_suites, i_suites_len, add_suite_iptools_parse_mac);
    add_suites(fun_suites, i_suites_len, add_suite_iptools_parse_portrange);

    //t_iconv2utf8
    add_suites(fun_suites, i_suites_len, add_suite_iconv2utf8);

    //t_frmem_stat
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_all_used);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_destroy);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_free);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_info);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_info_all);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_init);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_malloc);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_memdup);
//    add_suites(fun_suites, i_suites_len, add_suite___fr_memstat_strdup);
    //t_frbyte_buffer
    add_suites(fun_suites, i_suites_len, add_suite__bytebuff_node_free);
    add_suites(fun_suites, i_suites_len, add_suite__packet_add);
    add_suites(fun_suites, i_suites_len, add_suite__packet_add_skip);
    add_suites(fun_suites, i_suites_len, add_suite__parse_endian);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_add);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_creat);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_free);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_bytes);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_bytes_at);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_char);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_int);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_long);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_get_short);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_getPosition);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_memstr);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_peek_bytes);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_peek_char);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_peek_int);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_peek_long);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_peek_short);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_print);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_remaining);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_rewind);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_setPosition);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_skip);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuff_truncate);
    add_suites(fun_suites, i_suites_len, add_suite_bytebuffer_get_at);   

    //t_frjson_checker
    add_suites(fun_suites, i_suites_len, add_suite_frjson_format_detect);

    for (;i_loop < i_suites_len; i_loop++)
    {
        if (fun_suites[i_loop] != NULL && fun_suites[i_loop]() != 0)
        {
            CU_cleanup_registry();
            return CU_get_error();
        }
    }

    apmtest_cunit_unregistry();
    fr_slab_destroy();

    return 0;
}
